#! /bin/bash

# 获取环境变量
PLAY_HOME=/opt/playground
config_tools=$PLAY_HOME/configs.sh

# 配置DS:standalone模式,common.properties配置文件
function configDS_common_standalone_for_3()
{
# 获取ds配置文件
ds_config=$1

echo "正在配置common.properties文件"

hadoopInfo=`egrep "^hadoop" $PLAY_HOME/frames.txt`
masterNode=`echo $hadoopInfo | cut -d " " -f4` 

sed -i "s/resource.storage.type=NONE/resource.storage.type=HDFS/g" $ds_config
sed -i "s/resource.hdfs.root.user=hdfs/resource.hdfs.root.user=root/g" $ds_config
sed -i "s/resource.hdfs.fs.defaultFS=hdfs:\/\/mycluster:8020/resource.hdfs.fs.defaultFS=hdfs:\/\/$masterNode:9000/g" $ds_config
}

# 初始化Mysql，调用时需输入参数为DS安装目录
function init_mysql_data()
{
 ds_home=$1

 echo "正在MySQL中创建数据库"

 mysqlNode=`egrep "^mysql-rpm-pack" $PLAY_HOME/frames.txt | cut -d " " -f3`
 mysqlHivePasswd=`egrep "^mysql-hive-password" $PLAY_HOME/configs.txt | cut -d " " -f2 | sed s/\r//`
 
 # 创建数据库dolphinscheduler，用于存放dolphinscheduler元数据
 ssh $mysqlNode "source /etc/profile && export MYSQL_PWD=$mysqlHivePasswd && mysql --connect-expired-password -uroot -e \"drop database if exists dolphinscheduler;\""
 ssh $mysqlNode "source /etc/profile && export MYSQL_PWD=$mysqlHivePasswd && mysql --connect-expired-password -uroot -e \"CREATE DATABASE dolphinscheduler DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;\""
 ssh $mysqlNode "source /etc/profile && export MYSQL_PWD=$mysqlHivePasswd && mysql --connect-expired-password -uroot -e \"GRANT ALL PRIVILEGES ON dolphinscheduler.* TO 'root'@'%' IDENTIFIED BY 'DBa2020*';flush privileges;\""
 ssh $mysqlNode "source /etc/profile && export MYSQL_PWD=$mysqlHivePasswd && mysql --connect-expired-password -uroot -e \"GRANT ALL PRIVILEGES ON dolphinscheduler.* TO 'root'@'localhost' IDENTIFIED BY 'DBa2020*';flush privileges;\""

 # 与用户交互获取驱动路径
 read -p "请输入mysql-connector-java-8.0.16.jar的路径（/root/mysql-connector-java-8.0.16.jar）: " mysql_driver_8
 mysql_driver_8=${mysql_driver_8:-"/root/mysql-connector-java-8.0.16.jar"}

 # 将配置信息存储到ds_mysql_driver变量中
 echo "将配置信息存储到ds_mysql_driver变量中"
 bash $config_tools add  "ds_mysql_driver" $mysql_driver_8
 echo "存储完毕"

 cp $mysql_driver_8 $ds_home/standalone-server/libs/
 cp $mysql_driver_8 $ds_home/api-server/libs/
 cp $mysql_driver_8 $ds_home/alert-server/libs/
 cp $mysql_driver_8 $ds_home/master-server/libs/
 cp $mysql_driver_8 $ds_home/worker-server/libs/
 cp $mysql_driver_8 $ds_home/tools/libs/
}

# 配置DS:standalone模式,dolphinscheduler_env.sh配置文件
function configDS_env_standalone_for_3()
{

# 获取ds配置文件
ds_config=$1

mysqlNode=`egrep "^mysql-rpm-pack" $PLAY_HOME/frames.txt | cut -d " " -f3`
mysqlHivePasswd=`egrep "^mysql-hive-password" $PLAY_HOME/configs.txt | cut -d " " -f2 | sed s/\r//`

export DATABASE=${DATABASE:-postgresql}
export SPRING_PROFILES_ACTIVE=${DATABASE}
export SPRING_DATASOURCE_URL
export SPRING_DATASOURCE_USERNAME
export SPRING_DATASOURCE_PASSWORD

sed -i "s/export DATABASE=\${DATABASE:-postgresql}/export DATABASE=mysql/g" $ds_config
sed -i "s/export SPRING_DATASOURCE_URL/export SPRING_DATASOURCE_URL=\"jdbc:mysql:\/\/$mysqlNode:3306\/dolphinscheduler\?useUnicode=true\&characterEncoding=UTF-8\&useSSL=false\"/g" $ds_config
sed -i "s/export SPRING_DATASOURCE_USERNAME/export SPRING_DATASOURCE_USERNAME=root/g" $ds_config
sed -i "s/export SPRING_DATASOURCE_PASSWORD/export SPRING_DATASOURCE_PASSWORD=$mysqlHivePasswd/g" $ds_config

}

function installDS()
{
 #1.在frames.txt中查看是否需要安装ds
 dsInfo=`egrep "dolphinscheduler" $PLAY_HOME/frames.txt`
 
 ds=`echo $dsInfo | cut -d " " -f1`
 isInstall=`echo $dsInfo | cut -d " " -f2`
 installNode=`echo $dsInfo | cut -d " " -f3`
 master_node=`echo $dsInfo | cut -d " " -f4`
 node=`hostname`

 
 #是否安装
 if [[ $isInstall = "true" && $installNode =~ $node ]];then
    
    #2.查看$PLAY_HOME/frames目录下是否有DS安装包
    dsIsExists=`find $PLAY_HOME/frames -name $ds`
    if [[ ${#dsIsExists} -ne 0 ]];then
        if [[ ! -d /opt/app ]];then
            mkdir /opt/app && chmod -R 775 /opt/app
        fi
   
        #删除旧的
        ds_home_old=`find /opt/app -maxdepth 1 -name "*dolphinscheduler*"`
        for i in $ds_home_old;do
            rm -rf $i
        done

        #3.解压到指定文件夹/opt/app中
        echo "开始解压dolphinscheduler安装包"
        
        tar -zxvf $dsIsExists -C /opt/app >& /dev/null
        echo "dolphinscheduler安装包解压完毕"

        version=$(echo "$ds" | grep -oP '(?<=apache-dolphinscheduler-)\d+\.\d+\.\d+')
        ds_home=$(find /opt/app -maxdepth 1 -name "*dolphinscheduler*" | grep "$version")


        # 检查版本号并调用对应的配置方法
        if [[ "$version" =~ ^3\. ]]; then
            # 配置common.properties
            configDS_common_standalone_for_3 $ds_home/standalone-server/conf/common.properties
            # 初始化mysql数据库数据
            init_mysql_data $ds_home
            # 配置dolphinscheduler_env.sh将数据库切换到mysql
            configDS_env_standalone_for_3 $ds_home/bin/env/dolphinscheduler_env.sh
            configDS_env_standalone_for_3 $ds_home/standalone-server/conf/dolphinscheduler_env.sh
            # 执行脚本生成数据库数据
            chmod +x $ds_home/tools/bin/upgrade-schema.sh
            $ds_home/tools/bin/upgrade-schema.sh

        else
            echo "目前脚本只适配3.x版本，其它版本暂未适配"
        fi

        #4.配置ES_HOME
        profile=/etc/profile
        sed -i "/^export DS_HOME/d" $profile
        echo "export DS_HOME=$ds_home" >> $profile

        #5.配置PATH
        sed -i "/^export PATH=\$PATH:\$DS_HOME\/bin/d" $profile
        echo "export PATH=\$PATH:\$DS_HOME/bin:" >> $profile

        #6.更新/etc/profile文件
        source /etc/profile && source /etc/profile

        echo "DS安装完成！"

    else
        echo "$PLAY_HOME/frames目录下没有DS安装包"
    fi
 else
     echo "DS不允许被安装在当前节点"
 fi

}

function uninstallDS()
{

    # 获取DS安装目录
    dsInfo=`egrep "dolphinscheduler" $PLAY_HOME/frames.txt`
    ds=`echo $dsInfo | cut -d " " -f1`

    version=$(echo "$ds" | grep -oP '(?<=apache-dolphinscheduler-)\d+\.\d+\.\d+')
    ds_home=$(find /opt/app -maxdepth 1 -name "*dolphinscheduler*" | grep "$version")

    # 移除DS_HOME环境变量
    sed -i "/^export DS_HOME/d" /etc/profile
    sed -i "/^export PATH=\$PATH:\$DS_HOME\/bin/d" /etc/profile

    # 更新/etc/profile文件
    source /etc/profile

    # 将DS从系统中移除
    rm -rf $ds_home

    echo "Dolphinscheduler已成功卸载"
}

# 判断输入参数并执行相应操作
if [ "$1" == "install" ]; then
    installDS
elif [ "$1" == "uninstall" ]; then
    uninstallDS
else
    echo "无效的参数"
fi